#!/bin/bash

# PBJ Server Database Table Creation Script (Cross-Platform)
# Author: PBJ Server Team
# Created: 2024
# Compatible with: Linux, macOS, WSL

set -e  # Exit on any error

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to print colored output
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

echo "========================================"
echo "PBJ Server Database Table Creation Script"
echo "========================================"
echo

# Check if MySQL client is installed
if ! command -v mysql &> /dev/null; then
    print_error "MySQL client not found, please ensure MySQL is installed and added to PATH"
    print_info "Please visit: https://dev.mysql.com/downloads/mysql/"
    exit 1
fi

print_success "MySQL client detected"
echo

# Database configuration
DB_HOST=${DB_HOST:-"localhost"}
DB_PORT=${DB_PORT:-"3306"}
DB_USER=${DB_USER:-"admin"}
DB_PASS=${DB_PASS:-"123456"}
DB_NAME=${DB_NAME:-"pbj_server"}

print_info "Database configuration:"
echo "  Host: $DB_HOST"
echo "  Port: $DB_PORT"
echo "  User: $DB_USER"
echo "  Database: $DB_NAME"
echo

# Function to execute MySQL command with error handling
execute_mysql() {
    local query="$1"
    local description="$2"
    
    if mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" -e "$query" 2>/dev/null; then
        print_success "$description"
        return 0
    else
        print_error "$description failed"
        return 1
    fi
}

# Step 1: Create database (if not exists)
print_info "Step 1: Creating database..."
if execute_mysql "CREATE DATABASE IF NOT EXISTS $DB_NAME CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" "Database creation/confirmation"; then
    echo
else
    print_error "Failed to create database, please check database connection information"
    exit 1
fi

# Step 2: Create data tables
print_info "Step 2: Creating data tables..."
if [ -f "create_tables.sql" ]; then
    if mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" < create_tables.sql 2>/dev/null; then
        print_success "Data table creation completed"
    else
        print_error "Failed to execute SQL script"
        exit 1
    fi
else
    print_error "create_tables.sql file not found"
    exit 1
fi
echo

# Step 3: Verify table creation
print_info "Step 3: Verifying table creation results..."
if execute_mysql "SHOW TABLES;" "Table creation verification"; then
    echo
else
    print_warning "Unable to verify table creation results"
fi

# Display default admin account information
print_info "Default admin account:"
echo "  Username: admin"
echo "  Email: admin@pbj.com"
echo "  Phone: 13800138000"
echo "  Password: 123456"
echo "  Role: admin"
echo

echo "========================================"
print_success "Database initialization completed!"
print_info "You can now start PBJ Server"
echo "========================================"
echo

# Optional: Show how to start the server
print_info "To start PBJ Server, run one of the following commands:"
echo "  go run main.go"
echo "  make run"
echo "  go build -o pbj-server main.go && ./pbj-server"
echo 